// pages/detail_two/detail_two.js
import {
  getTimeWithId
} from '../../utils/util.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title_url: '/images/detail/carousel/02.jpg',
    click_tab_index: 0,
    // tab_list: ['tab1', 'tab2'],
    content_list: [{
      contents: [{
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content1',
        url: '/pages/detail_one/detail_one'
      }, {
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content1',
        url: '/pages/detail_one/detail_one'
      }, {
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content1',
        url: '/pages/detail_one/detail_one'
      }, {
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content1',
        url: '/pages/detail_one/detail_one'
      }, {
        imgs: ['/images/detail/carousel/02.jpg'],
        title: 'content1',
        url: '/pages/detail_one/detail_one'
      }]
    }, {
      contents: [{
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content2',
        url: '/pages/detail_one/detail_one'
      }, {
        image_url: '/images/detail/carousel/02.jpg',
        text: 'content2',
        url: '/pages/detail_one/detail_one'
      }]
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    const eventChannel = this.getOpenerEventChannel()
    const that = this
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromIndexPage', function(data) {
      const {
        barName,
        tab_list
      } = data

      // console.log(data)

      // 设置导航栏标题文字
      wx.setNavigationBarTitle({
        title: barName
      })

      that.setData({
        ...data
      })
    })
  },
  /**
   * 点击tab
   */
  onTabEvent: function(e) {
    const {
      index
    } = e.detail
    // console.log(index)
    this.setData({
      click_tab_index: index
    })
  },
  /**
   * 点击内容，跳转到detail_one
   */
  clickContent: function(e) {
    const {
      index
    } = e.currentTarget.dataset

    const {
      barName,
      click_tab_index,
      content_list
    } = this.data

    const tempData = content_list[click_tab_index][index]
    // console.log(tempData)
    const {
      _id
    } = tempData
    // console.log(_id)
    const time = getTimeWithId(_id)
    // console.log(_id, time)

    wx.navigateTo({
      url: `/pages/detail_one/detail_one`,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromIndexPage', {
          ...tempData,
          barName,
          time
        })
      }
    })
  },
  /**
   * 资讯中心的item
   */
  toAskItem: function (item) {
    // console.log('资讯中心item', item)
    const {
      index
    } = item.currentTarget.dataset

    this.toDetailOneFormatData(index, 'listAsk')
  },
  toDetailOneFormatData: function (index, listIndex) {
    // console.log('案例展示下面的item', item)
    // const {
    //   index
    // } = item.currentTarget.dataset

    const info = app.globalData.indexData[listIndex][index]
    const barName = this.data.barName

    //     barName: "永昌服务"
    //     img_list: Array(1)
    //     0: "image-1576142434353.png"
    //     text:
    //     article: "<p>无</p>"
    //     title: "设计/Design"
    //     time: 1575364207000

    // console.log(info)

    let newInfo = {
      ...info,
      barName
    }
    newInfo.imgs = info.img_list ? info.img_list.map(item => (app.globalData.BASE_IMG_URL + item)) : null
    newInfo.img_list = null
    newInfo.article = info.text.article
    newInfo.articleText = info.text.articleText ? info.text.articleText : null
    newInfo.title = info.text.title
    newInfo.time = info.time ? info.time : null

    wx.navigateTo({
      url: '/pages/detail_one/detail_one',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromIndexPage', {
          ...newInfo
        })
      }
    })
  }
})